home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QD3D / rollercoaster / Sources / TextureMap.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.0 KB  |  222 lines  |  [TEXT/dosa]

  1. /*
  2.     File:        TextureMap.c
  3.     
  4.     Contains:    Contains code to create a QD3D shader object
  5.     
  6.     Written by:    Scott Kuechle, based on original Gerbils code by Brian Greenstone
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc. All rights reserved
  9.     
  10.     Change History (most recent first)
  11.     
  12.         <1>        9/1/98        srk        first file
  13.  
  14.  
  15. */
  16.  
  17. /************************************************************
  18. *                                                           *
  19. *    INCLUDE FILES                                          *
  20. *                                                           *
  21. *************************************************************/
  22.  
  23. #include "TextureMap.h"
  24.  
  25.  
  26. /************************************************************
  27. *                                                           *
  28. *    FUNCTION PROTOTYPES                                    *
  29. *                                                           *
  30. *************************************************************/
  31.  
  32. static void TextureMap_CreateTexturePixmap(PicHandle pict, short mapSizeX, short mapSizeY, TQ3StoragePixmap *bMap);
  33. static void TextureMap_TexturePixmap(PicHandle pict,short mapSizeX, short mapSizeY, TQ3StoragePixmap *bMap);
  34.  
  35.  
  36. /************************************************************
  37. *                                                           *
  38. *    FUNCTION:  TextureMap_Get                              *
  39. *                                                           *
  40. *    PURPOSE:   Loads a PICT resource and returns a shader  *
  41. *               object which is based on the PICT          *
  42. *               PICT converted to a texture map.            *
  43. *                                                           *
  44. *                                                           *
  45. *************************************************************/
  46.  
  47. TQ3ShaderObject    TextureMap_Get(PicHandle picH, Rect *picRect)
  48. {
  49.     TQ3StoragePixmap     pixmap;
  50.     TQ3TextureObject    texture;
  51.     TQ3ShaderObject        shader = NULL;
  52.         
  53.  
  54.         if (picH != NULL)
  55.         {
  56.                     /* MAKE INTO STORAGE PIXMAP */
  57.             pixmap.image = NULL;
  58.             TextureMap_TexturePixmap (picH,
  59.                                     (short)(picRect->right  - picRect->left),
  60.                                     (short)(picRect->bottom - picRect->top),
  61.                                     &pixmap);
  62.             if (pixmap.image == NULL)
  63.             {
  64.                 Utils_DisplayErrorMsg("Error Texturing Pixmap!");
  65.                 goto bail;
  66.             }
  67.                     /* MAKE NEW PIXMAP TEXTURE */
  68.                     
  69.             texture = Q3PixmapTexture_New (&pixmap);
  70.             if (texture == nil)
  71.             {
  72.                 Utils_DisplayErrorMsg("Error calling Q3PixmapTexture_New!");
  73.                 goto bail;
  74.             }
  75.                 
  76.             shader = Q3TextureShader_New (texture);
  77.             if (shader == nil)
  78.             {
  79.                 Utils_DisplayErrorMsg("Error calling Q3TextureShader_New!");
  80.                 goto bail;
  81.             }
  82.             Q3Object_Dispose (texture);
  83.                 /* disposes of extra reference to storage obj from CreateTexturePixmap */
  84.             Q3Object_Dispose (pixmap.image);
  85.  
  86.         }
  87.  
  88.  
  89.         return( shader );
  90.         
  91.         bail:
  92.             if (texture)
  93.             {
  94.                 Q3Object_Dispose (texture);
  95.             }
  96.             if (pixmap.image)
  97.             {
  98.                 Q3Object_Dispose (pixmap.image);    // disposes of extra reference to storage obj from CreateTexturePixmap
  99.             }
  100.             
  101.             return ( NULL );
  102. }
  103.  
  104.  
  105.  
  106.  
  107. /************************************************************
  108. *                                                           *
  109. *    FUNCTION:  TextureMap_TexturePixmap                    *
  110. *                                                           *
  111. *    PURPOSE:   Create a new memory storage object based    *
  112. *               on the PICT file passed as an input         *
  113. *                                                           *
  114. *                                                           *
  115. *                                                           *
  116. *************************************************************/
  117.  
  118. static void TextureMap_TexturePixmap(PicHandle pict,
  119.                                     short mapSizeX,
  120.                                     short mapSizeY,
  121.                                     TQ3StoragePixmap *bMap)
  122. {
  123.     unsigned long             pictMapAddr;
  124.     Rect                     rectGW;
  125.     GWorldPtr                 pGWorld = NULL;
  126.     PixMapHandle             hPixMap = NULL;
  127.     unsigned long             pictRowBytes;
  128.     OSErr                    myErr;
  129.     GDHandle                oldGD;
  130.     GWorldPtr                oldGW;
  131.     long                    width, height;
  132.     Boolean                    success = false;
  133.  
  134.         bMap->image = NULL;
  135.         
  136.         if (mapSizeY > 512)
  137.         {
  138.             mapSizeY = 512;
  139.         }
  140.         if (mapSizeX > 512)
  141.         {
  142.             mapSizeX = 512;
  143.         }
  144.  
  145.         GetGWorld(&oldGW, &oldGD);            /* save current port */
  146.  
  147.  
  148.         MacSetRect(&rectGW, 0, 0, mapSizeX, mapSizeY);        /* set dimensions */
  149.         width = rectGW.right - rectGW.left;
  150.         height = rectGW.bottom - rectGW.top;
  151.  
  152.         myErr = NewGWorld(&pGWorld, 0, &rectGW, 0, 0, 0L);            /* make gworld */
  153.         if (myErr != noErr)
  154.         {
  155.             Utils_DisplayErrorMsg("Offscreen GWorld creation failed!");
  156.             goto bail;
  157.         }
  158.         
  159.         hPixMap = GetGWorldPixMap(pGWorld);            /* calc addr & rowbytes */
  160.         if (hPixMap != NULL)
  161.         {
  162.             pictMapAddr = (unsigned long)GetPixBaseAddr(hPixMap);
  163.             pictRowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;
  164.  
  165.                     /* DRAW PICTURE INTO GWORLD */
  166.                     
  167.             SetGWorld(pGWorld, nil);    
  168.             success = LockPixels(hPixMap);
  169.             if (success == false)
  170.             {
  171.                 Utils_DisplayErrorMsg("Offscreen pixel image has been purged!");
  172.                 goto bail;
  173.             }
  174.             
  175.             EraseRect(&rectGW);
  176.             DrawPicture(pict, &rectGW);
  177.  
  178.                         /* SET MORE PIXELMAP INFO */
  179.  
  180.             bMap->image = Q3MemoryStorage_New ((unsigned char *) pictMapAddr, pictRowBytes * mapSizeY);
  181.             if( bMap->image != NULL)
  182.             {
  183.                 PixMapHandle pmHndl;
  184.                 
  185.                     pmHndl = pGWorld->portPixMap;
  186.                 
  187.                     bMap->width     = mapSizeX;
  188.                     bMap->height    = mapSizeY;
  189.                     bMap->rowBytes     = pictRowBytes;
  190.                     bMap->pixelSize = (**pmHndl).pixelSize;    /* use current pixel size */
  191.                     bMap->pixelType    = ((**pmHndl).pixelSize == 16) ? kQ3PixelTypeRGB16 : kQ3PixelTypeRGB32;
  192.                     
  193.                     #if TARGET_OS_MAC
  194.                         bMap->bitOrder    = kQ3EndianBig;
  195.                         bMap->byteOrder    = kQ3EndianBig;
  196.                     #else if TARGET_OS_WIN32
  197.                         bMap->bitOrder    = kQ3EndianLittle;
  198.                         bMap->byteOrder    = kQ3EndianLittle;
  199.                     #endif
  200.             }
  201.             else
  202.             {
  203.                 Utils_DisplayErrorMsg("Could not create new memory storage object!");
  204.             }
  205.         }
  206.     
  207.     bail:
  208.     
  209.         /* Clean up */
  210.         SetGWorld (oldGW, oldGD);
  211.         
  212.         if (hPixMap != NULL)
  213.         {
  214.             UnlockPixels (hPixMap);
  215.         }
  216.         if (pGWorld != NULL)
  217.         {
  218.             DisposeGWorld (pGWorld);
  219.         }
  220.         
  221. }
  222.